<?php
namespace Home\Model;
use Think\Model;
class NoticeModel extends Model {
    public function addNotice($data){
        $this->add($data);
    }

    public function deleteCommentNoticeByArticleId($article_id){
        //要Notice必须在comment之前删除
        $comments=D('Comment')->getCommentByArticleId($article_id);
        foreach($comments as $key=>$comment){
            $notice_id=$comment['id'];
            $this->where("notice_type='comment' and notice_id='$notice_id'")->delete();
        }

    }

    public function deleteNoticeByUserId($user_id){
        $this->where("from_id='$user_id'  or to_id='$user_id'")->delete();
    }
    public function deleteNoticeByCommentId($comment_id){
        $this->where("notice_id='$comment_id' and notice_type='comment'")->delete();
    }


    public function updateNoticeByMessageId($message_id,$data){
//        dump($message_id);getMessage_NoticeByToId
//        dump($data);
        $this->where("notice_id='$message_id'")->data($data)->save();
    }

    public function getAllNotice(){
        return $this->select();
    }

    public function getNoticeById($id){
        return $this->where("id='$id'")->select();
    }

    public function getNoticeByFromId($id){
        return $this->where("from_id='$id'")->select();
    }

    public function getNoticeByToId($id){
        return $this->where("to_id='$id'")->select();
    }

    public function getMessage_NoticeByToId($id){
//        dump($this->where("to_id='$id' and notice_type='message'")->select());
        return $this->where("to_id='$id' and notice_type='message'")->select();
    }

    public function getViewedComment_NoticeByToId($id){
//        dump($this->where("to_id='$id' and notice_type='comment' and view_status='true'")->buildSql());die();
        return $this->where("to_id='$id' and notice_type='comment' and view_status='true'")->select();
    }

    public function getUnViewedComment_NoticeByToId($id){
//        dump($this->where("to_id='$id' and notice_type='comment' and view_status='false'")->buildSql());
        return $this->where("to_id='$id' and notice_type='comment' and view_status='false'")->select();
    }

    public function getComment_NoticeByToId($id){
        return $this->where("to_id='$id' and notice_type='comment'")->select();
    }

    public function getUnViewCommentNoticeByToId($id){
        return $this->where("to_id='$id' and notice_type='comment' and view_status='false'")->select();
    }

    public function getAllNoticeNumber(){
        $uid=$_SESSION['uid'];
        $messages=D('Message')->getDistinctMessageByNoticeToId($uid);
        $comments=D('Comment')->getDistinctCommentByNoticeToId($uid);
        return $messages['notice_count']+$comments['notice_count'];
    }


}